package person.bangbang.im.Androidgin.Framework;

import person.bangbang.im.Androidgin.R;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
/**
 * sound subsystem.
 *
 * @author bangbang.song@gmail.com
 */
public class Sound implements OnCompletionListener {
	public static final int ID_BUDDY_ARRIVE = 1; //	Buddy signs on.

	public static final int ID_BUDDY_LEAVE = 2; //	Buddy signs off.

	public static final int ID_RECEIVE = 3; //	Receive an IM.

	public static final int ID_FIRST_RECEIVE = 4; // 	Receive an IM that starts a conv.

	public static final int ID_SEND = 5; //	Send an IM.

	public static final int ID_CHAT_JOIN = 6; //	Someone joins a chat.

	public static final int ID_CHAT_LEAVE = 7; //Someone leaves a chat.

	public static final int ID_CHAT_YOU_SAY = 8; //	You say something in a chat.

	public static final int ID_CHAT_SAY = 9; //	Someone else says somthing in a chat.

	public static final int ID_POUNCE_DEFAULT = 10; //	Default sound for a buddy pounce.

	public static final int ID_CHAT_NICK = 11; //	Someone says your name in a chat.
	
	public static final int ID_LOGIN = 12;
	
	public static final int ID_LOGOUT = 13;
 
	private static Sound _instance;
	private Context mCon;
	private MediaPlayer mPlayer;
	private Sound(Context con) {
		this.mCon = con;
	}
	public void init() {
		// TODO how to user mPlayer with res id and without context.
		mPlayer = new MediaPlayer();
		mPlayer.setOnCompletionListener(new OnCompletionListener () {

			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				mp.release();
			}
			
		});
	}
	public static Sound getInstance(Context con) {
		if (null == _instance) {
			_instance = new Sound(con);
			_instance.init();
		}
		return _instance;
	}
	/** 
	 * play a sound by the eventId
	 * @param id
	 */
	public void playEvent(int id) {
		switch (id) {
		case ID_RECEIVE:
		case ID_BUDDY_ARRIVE:
		case ID_CHAT_JOIN:
		case ID_FIRST_RECEIVE:
			mPlayer = MediaPlayer.create(mCon, R.raw.receive);

			break;
		case ID_SEND:
		case ID_BUDDY_LEAVE:
		case ID_CHAT_LEAVE:
			mPlayer = MediaPlayer.create(mCon, R.raw.send);
			
			break;
		case ID_CHAT_NICK:
		case ID_CHAT_YOU_SAY:
		case ID_CHAT_SAY:
		case ID_POUNCE_DEFAULT:
			mPlayer = MediaPlayer.create(mCon, R.raw.alert);
			
			break;
		case ID_LOGIN:
			mPlayer = MediaPlayer.create(mCon, R.raw.login);
			
			break;
		case ID_LOGOUT:
			mPlayer = MediaPlayer.create(mCon, R.raw.logout);
			
			break;

		default:
			break;
		}
		
		mPlayer.setOnCompletionListener(this);
		mPlayer.start();
	}
	
	public void onCompletion(MediaPlayer mp) {
		// TODO Auto-generated method stub
		mp.release();
	}
}
